package com.hz.filemanager.ui.clean;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.airbnb.lottie.LottieAnimationView;
import com.hz.filemanager.App;
import com.hz.filemanager.BuildConfig;
import com.hz.filemanager.R;
import com.hz.filemanager.ad.AdLoadManager;
import com.hz.filemanager.ad.utils.AnalyticsUtils;
import com.hz.filemanager.model.FileDataManager;
import com.hz.filemanager.ui.base.InterruptActivity;
import com.hz.sdka.ada.AdListenera;
import com.hz.sdka.ada.Interstitiala;
import com.hz.sdka.commona.bean.HZAdError;


public class AnimationActivity extends InterruptActivity {

    private Toolbar toolbar;
    private LottieAnimationView av;


    private final static int ANIMATION_END = 100;
    public final static String ALLSIZE = "allsize";

    private int cleanSize;
    private int delayNum = 0;
    private boolean isReady = false;

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case ANIMATION_END:
                    if(isQuit) return;
                    if(type == TYPE_CLEAN_REP
                            || type == TYPE_BOOST_REP
                            || type == TYPE_COOLER_REP
                            || type == TYPE_BATTERY_REP){
                        //重复使用功能 不在这里弹广告
                        gotoCleanResult(type);
                        return;
                    }

                    delayNum++;
                    if(Interstitiala.getInstance().isReady() || delayNum > 1){
                        if(Interstitiala.getInstance().isReady()){

                            if(!isReady){
                                isReady = true;
                                AnalyticsUtils.INSTANCE.logEvent("gongneng_ad_ready_"+ getActionWithType(type));
                                AnalyticsUtils.INSTANCE.logEvent("gongneng_ad_ready_time_" + delayNum*3 + "s");
                            }
                            showAD();
                        }
                        gotoCleanResult(type);
                    }else{
                        handler.sendEmptyMessageDelayed(ANIMATION_END, 3000);
                    }
                    break;
            }
        }
    };

    private void showAD() {
        if(FileDataManager.appConfig.getFile_Config().getDxjs_ad()==1){
            AdLoadManager.INSTANCE.showInterstitialAd(this, BuildConfig.CJ_SPLASH_AD, new AdListenera() {
                @Override
                public void adError(HZAdError adError) {

                }

                @Override
                public void adClose() {

                }

                @Override
                public void adShow() {
                    App.animationADShow = true;
                    AnalyticsUtils.INSTANCE.logEvent("gongneng_ad_succeed_" + getActionWithType(type));
                }
            });
        }
    }

    private void gotoCleanResult(int type) {
        Intent intent = new Intent(AnimationActivity.this, CleanResultActivity.class);
        if(type == TYPE_CLEAN){
            intent.putExtra(CleanResultActivity.CLEAN_SIZE, cleanSize);
        }
        intent.putExtra(CleanResultActivity.TYPE, type);
        startActivity(intent);
        finish();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_animation;
    }

    @Override
    protected void initView() {
        toolbar = findViewById(R.id.toolbar);
        av = findViewById(R.id.lottie_av);
    }

    @Override
    protected void initEvent() {


        AnalyticsUtils.INSTANCE.logEvent("gongneng_ad_"+ getActionWithType(type));

        type = getIntent().getIntExtra(TYPE, -1);
        cleanSize = getIntent().getIntExtra(ALLSIZE, 0);
        switch (type){
            case TYPE_CLEAN:
            case TYPE_CLEAN_REP:
                toolbar.setTitle(R.string.junk_clean);
                if(type == TYPE_CLEAN_REP){
                    av.setAnimation("rep/data.json");
                }else{
                    av.setImageAssetsFolder("clean/images");
                    av.setAnimation("clean/data.json");
                }

                break;
            case TYPE_BOOST:
            case TYPE_BOOST_REP:
                toolbar.setTitle(R.string.phone_boost);
                if(type == TYPE_BOOST_REP){
                    av.setAnimation("rep/data.json");
                }else{
                    av.setImageAssetsFolder("boost/images");
                    av.setAnimation("boost/data.json");
                }

                break;
            case TYPE_COOLER:
            case TYPE_COOLER_REP:
                toolbar.setTitle(R.string.phone_cooler);
                if(type == TYPE_COOLER_REP){
                    av.setAnimation("rep/data.json");
                }else{
                    av.setImageAssetsFolder("cooler/images");
                    av.setAnimation("cooler/data.json");
                }

                break;
            case TYPE_BATTERY:
            case TYPE_BATTERY_REP:
                toolbar.setTitle(R.string.battery_saver);
                if(type == TYPE_BATTERY_REP){
                    av.setAnimation("rep/data.json");

                }else{
                    av.setImageAssetsFolder("battery/images");
                    av.setAnimation("battery/data.json");
                }
                break;

        }

        av.playAnimation();

        //判断是否有广告
        if(!Interstitiala.getInstance().isReady()){
            //load AD
            AdLoadManager.INSTANCE.loadInterstitial();
        }else{
            isReady = true;
            AnalyticsUtils.INSTANCE.logEvent("gongneng_ad_ready_"+ getActionWithType(type));
            AnalyticsUtils.INSTANCE.logEvent("gongneng_ad_ready_time_" + delayNum*3 + "s");
        }

        handler.sendEmptyMessageDelayed(ANIMATION_END, 3000);
    }

    @Override
    protected void onDestroy() {
        if(handler!=null){
            handler.removeCallbacksAndMessages(null);//清空消息队列
        }
        super.onDestroy();
    }
}
